﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UI;
using System;
using ProtoBuf.Message;
public static class StaticData
{
    #region  玩家基本数据
    public static UIPlayerData playerData = new UIPlayerData();//玩家实例数据
    public static ChatMessageCenter chatMessageCenter = new ChatMessageCenter();

    public static BaseAttributeData unionAttributeConst = new BaseAttributeData (0);
    public static BaseAttributeData unionAttributePer = new BaseAttributeData(0);

    public static BaseAttributeData unionChurchAttributeConst = new BaseAttributeData(0);
    public static BaseAttributeData unionChurchAttributePer = new BaseAttributeData(0);
    public static SceneType currentSceneType
    {
        get
        {
            return Room.RoomManager.curSceneType;
        }
    }
    //public static SceneType sceneType = SceneType.LOGIN;
    //public static SceneParameter sceneParameter = new SceneParameter();

    public static byte[] sessionKey;
    public static string socketIp;
    public static int socketPort;
    public static string httpServer;

    private static int loadTime = 0;
    public static RedPointTip redTips = new RedPointTip();//本地的红点数据结构
    public static int maxPlayerInView = 100;
    //public static bool hasLevelUp = false;

    //public static void InitPlayerData(ProtoBuf.Message.SCPlayerLogin data)
    //{
    //    playerData.InitData(data);
    //}
    public static void InitPlayerData(ProtoBuf.Message.SCPlayerLogin player, ProtoBuf.Message.SCEquipmentTreasureList equipList, ProtoBuf.Message.SCWarriorList warriorList, ProtoBuf.Message.SCItemList itemList, ProtoBuf.Message.SCFragmentList frageMentList, ProtoBuf.Message.SCFormationList formationList,ProtoBuf.Message.SCChangeNameTimes changeNameTimes, ProtoBuf.Message.SCGemAndCookingList cookingList,ProtoBuf.Message.SCPetList petList) 
    {
       playerData.InitData(player, equipList, warriorList, itemList, frageMentList, formationList, changeNameTimes, cookingList,petList);
    }

    public static void GetUnionAttribute(BaseProp result)
    {
        unionAttributePer.Reset();
        unionAttributePer.AddProp(AttributeQueue.Attack, result.peratt * CommonMethod.tableAttributeConvert);
        unionAttributePer.AddProp(AttributeQueue.Hp, result.perhp * CommonMethod.tableAttributeConvert);
        unionAttributePer.AddProp(AttributeQueue.MagicDefend, result.permdef * CommonMethod.tableAttributeConvert);
        unionAttributePer.AddProp(AttributeQueue.PhyDefend, result.perpdef * CommonMethod.tableAttributeConvert);
        unionAttributeConst = new BaseAttributeData(result);
    }
    public static void SetUnionChurchAttribute(BaseProp result)
    {
        unionChurchAttributePer.Reset();
        unionChurchAttributePer.AddProp(AttributeQueue.Attack, result.peratt * CommonMethod.tableAttributeConvert );
        unionChurchAttributePer.AddProp(AttributeQueue.Hp, result.perhp * CommonMethod.tableAttributeConvert );
        unionChurchAttributePer.AddProp(AttributeQueue.MagicDefend, result.permdef * CommonMethod.tableAttributeConvert );
        unionChurchAttributePer.AddProp(AttributeQueue.PhyDefend, result.perpdef * CommonMethod.tableAttributeConvert);
    }
    public static bool isFirstEnterGame
    {
        get
        {
            var v = PlayerPrefs.GetInt("FirstGame", 1);
            return v == 1 ? true : false;
        }

        set
        {
            PlayerPrefs.SetInt("FirstGame", value ? 1 : 0);
        }
    }

    private const string USER_NAME = "userName";
    private const string PASS_WORD = "passWord";
    public static string UserName
    {
        get { return PlayerPrefs.GetString(USER_NAME, ""); }
        set { PlayerPrefs.SetString(USER_NAME, value.ToString()); }
    }

    public  static string PassWord
    {
        get { return PlayerPrefs.GetString(PASS_WORD, ""); }
        set { PlayerPrefs.SetString(PASS_WORD, value.ToString()); }
    }

    #region 公会祈祷
    public static void PrayEnd()
    {
        NetAPI.SendHttp(OpDefine.CSUnionLightChurchInfo, new CSUnionLightChurchInfo(), (obj, ok) =>
        {
            if (ok)
            {
                ProtoBuf.Message.SCUnionLightChurchInfo result = obj.kBody as ProtoBuf.Message.SCUnionLightChurchInfo;
                StaticData.SetUnionChurchAttribute(result.addProp);
            }
        });       
    }
    public static void UnionAttribute()
    {
        if (StaticData.playerData.needRefreshUnionProp == 1)
        {
            ProtoBuf.Message.CSGetUnionAddBaseProp msg = new ProtoBuf.Message.CSGetUnionAddBaseProp();
            NetAPI.SendHttp(ProtoBuf.Message.OpDefine.CSGetUnionAddBaseProp, msg, (obj, ok) =>
            {
                if (ok)
                {
                    ProtoBuf.Message.SCGetUnionAddBaseProp result = obj.kBody as ProtoBuf.Message.SCGetUnionAddBaseProp;
                    StaticData.GetUnionAttribute(result.unionAddBaseProp);
                    StaticData.SetUnionChurchAttribute(result.unionPrayProp);
                    StaticData.playerData.needRefreshUnionProp = 0;
                }
            });
        }
    }
    #endregion

    #endregion

    #region GameSetting
    public static bool isMusicOn {
        get { return AudioManager.Instance.GetBackgroundMusic; }
        set { AudioManager.Instance.SetBackgroundMusic(value); }
    }
    public static bool isSoundOn {
        get { return AudioManager.Instance.GetSoundEffect; }
        set { AudioManager.Instance.SetSoundEffect(value); }
    }
    public static bool isShowOtherPlayerInTown;
    #endregion

    #region 副本数据
    public static UICopyData copyData = null;
    public static void InitCopyData()
    {
        copyData = new UICopyData();
        UnityEngine.Debug.Log("[DEBUG] copy data init !!");
    }
    public static List<CopyBoxInfo> boxs;
    public static List<CopyJobInfo> jobs;
    public static string missionScene;

    /// <summary>
    /// 当前战斗信息
    /// </summary>
    public static string currFightChapterId = "";
    public static string currFightLevelId = "";
    public static string currFightTaskId = "";

    public static List<BaseResource> willBaseDrops = new List<BaseResource>();

    public static bool IsFirstChapter1_1()
    {
        if (currFightChapterId == "1" && currFightLevelId == "1")
        {
            var level = UICopyData.GetUILevelData("2");
            if (level.level_inst_open)
                return false;
            else
            {
                if (currFightTaskId == "4")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        else {
            return false;
        }
    }

    public static bool IsFirstChapter1_1_3()
    {
        if (currFightChapterId == "1" && currFightLevelId == "1" && currFightTaskId == "4")
        {
            var level = UICopyData.GetUILevelData("2");
            if (level.level_inst_open)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }

    }

    public static int beforeBattleLevel = -1;

    #endregion

    public static BattleData curBattleData = new BattleData();
    public static bool isTestBattle = false;
    public static object lastBattleEndMsg;
    public static BalanceLeaveOption battleLeaveOption = BalanceLeaveOption.None;
    public static bool isLogining = false;//已经点过一次登录按钮了，正在进行登录
    public static bool isAutoSkipUltimate
    {
        get
        {
            return PlayerPrefs.GetInt("AutoSkipUltimate", 0) == 0 ? false : true;
        }
        set
        {
            PlayerPrefs.SetInt("AutoSkipUltimate", value ? 1 : 0);
        }
    }
    #region 公会战报名
    public static bool isAutoWarOn //自动宣战
    {
        get
        {
            return PlayerPrefs.GetInt("AutoWarOn", 0) == 0 ? false : true;
        }
        set
        {
             PlayerPrefs.SetInt("AutoWarOn", value ? 1 : 0);
        }
    }

    public static bool isAutoFight //战斗托管
    {
        get
        {
            return PlayerPrefs.GetInt("isAutoFight", 0) == 0 ? false : true;
        }
        set
        {
            PlayerPrefs.SetInt("isAutoFight", value ? 1 : 0);
        }
    }
    #endregion

    #region
    public static SCUFCurrentStageInfo currentInfo;
    #endregion
    #region PVE
    public static bool isLastBattleWin = false;
    public static bool isCompleteTask = false;
    public static bool isPveAutoCache = false;
    public static Battle.BattleSpeed pveSpeedCache = Battle.BattleSpeed._1x;
    public static bool isRelogin=false;
    #endregion

    #region PVP
    public static PVPRankInfo myPVPRankInfo = null;
    public static PVPRankInfo lastAttackerRankInfo = null;
    public static PVPRankInfo lastDefenderRankInfo = null;
    #endregion

    public static Battle.BattleSpeed pvpSpeedCache = Battle.BattleSpeed._1x;
    public static Battle.BattleSpeed pluderSpeedCache = Battle.BattleSpeed._1x;


    #region 配置
    public static EffectLevel effectLevel = EffectLevel.HIGH;
    #endregion

    #region 摄像旋转信息
    public static Vector3 cameraRotate = Vector3.zero;
    #endregion

    #region 剧情信息

    public static UIStoryData storyData = new UIStoryData();

    public static ResultObject storyTmpQuestGet = null;

    #endregion

    #region 新手引导
    public static List<GuideTagType> finishedTagGuides = new List<GuideTagType>();
    #endregion

    #region 监狱
    //玩家是否要进监狱
    public static bool isPlayerNeedJail = false;
    #endregion

    #region TownPage

    public static bool townNeedLogin = false;
    public static string townNeedFindQuestTarget = "";
    #endregion


    public static long clientServerTimeDelta = 0;
    public static string userIp = "";
    public static void InitTableData()
    {
        if (loadTime <= 0 || GameConfig.Instance.dataChange)
        {
            GameConfig.Instance.dataChange = false;
            loadTime = 1;
            System.DateTime dt = System.DateTime.Now;
            Dict.DictDataManager.Instance.Init(@"TableData");
            Debug.Log("<color=yellow>init table data cost time: "+(System.DateTime.Now - dt).TotalMilliseconds+"</color>");
        }
    }

    #region Rush
    public static int rushCurFloorNum = -1;
    public static int rushCurWinKey;
    public static int rushCurWinValue;
    public static int rushMaxFloor;
    public static string rushCurFloorSceneName = string.Empty;
    public static ProtoBuf.Message.SCRushGetSectionReward sectionReward = null;
    public static ProtoBuf.Message.SCRushGetSectionAddition sectionAddition = null;
    public static bool isMazeTraderNeedShow = false;
    public static bool isMazeTraderValid = false;
    private static int rushBossChanllegeTime = -1;
    private static List<ProtoBuf.Message.RushEliteBossInfo> eliteBossInfos = null;
    public static void ReceiveRushEliteBossInfo(ProtoBuf.Message.SCGetRushEliteBossInfo msg)
    {
        rushBossChanllegeTime = msg.leftTime;
        if (eliteBossInfos == null)
        {
            eliteBossInfos = msg.bossInfoList;
        }
        else
        {
            eliteBossInfos.Clear();
            eliteBossInfos.AddRange(msg.bossInfoList);
        }
    }

    public static bool IsRushBossInfoGot()
    {
        if (rushBossChanllegeTime == -1)
        {
            return false;
        }
        if (eliteBossInfos == null)
        {
            return false;
        }

        return true;
    }

    public static bool IsRushBossChallengeTipShow()
    {
        // 没开启
        if (StaticData.playerData.level < Dict.Blo.DictConfigBlo.GetRushLimitLevel())
        {
            return false;
        }
        // 没有初始数据
        if (!IsRushBossInfoGot())
        {
            return false;
        }

        bool isHaveBossOpen = false;
        foreach (var info in eliteBossInfos)
        {
            if (info.isOpen > 0)
            {
                isHaveBossOpen = true;
                break;
            }
        }

        if (!isHaveBossOpen)
        {
            return false;
        }

        if (rushBossChanllegeTime > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    #endregion

    #region Recycle
    public static ProtoBuf.Message.ResultObject recycleReward = null;

    public static int curRecycleResetSelectType;
    public static List<string> recycleResetSelect = new List<string>();
    public static void ClearRecycleResetSelect()
    {
        recycleResetSelect.Clear();
    }
    public static bool IsRecycleResetSelect()
    {
        return recycleResetSelect.Count > 0;
    }

    public static int curRecycleBreakSelectType;
    public static string[] recycleBreakupSelect = new string[4];
    public static int GetRecycleBreakupSelectNum()
    {
        int count = 0;
        for (int i = 0; i < 4; ++i)
        {
            if (!string.IsNullOrEmpty(recycleBreakupSelect[i]))
            {
                count++;
            }
        }
        return count;

    }
    public static void ClearRecycleBreakupSelect()
    {
        for (int i = 0; i < 4; ++i)
        {
            recycleBreakupSelect[i] = "";
        }
    }
    public static bool IsRecycleBreakupSelect()
    {
        foreach (var str in recycleBreakupSelect)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return true;
            }
        }
        return false;
    }

    #endregion

    #region 豪华装备

    public static List<int> listOpenedLuxuryEquipActivity = new List<int>();
    public static bool isOpeningLuxuryEquipActivity = false;

    #endregion
    public static void ClearAllData()
    {
        playerData.Clear();
        playerData = new UIPlayerData();
        if (copyData != null)
        {
            copyData.Clear();
        }
        copyData = null;
        chatMessageCenter.Clear();
        willBaseDrops.Clear();
        curBattleData.Clear();
        myPVPRankInfo = null;
        lastAttackerRankInfo = null;
        lastDefenderRankInfo = null;
        isLastBattleWin = false;
        isCompleteTask = false;
        //storyStep = 0;
        currFightChapterId = "";
        currFightLevelId = "";
        currFightTaskId = "";
        lastBattleEndMsg = null;
        battleLeaveOption = BalanceLeaveOption.None;
        beforeBattleLevel = -1;
        listOpenedLuxuryEquipActivity.Clear();
        funcNavKit.UnRegistNetHandler();
    }

    #region 野外数据
    public static UIFieldData fieldData = new UIFieldData();
    public static string resId;
    public static string resPosId;
    public static string fieldId;
    public static Dictionary<long, MovePlayer> playerInfos = new Dictionary<long, MovePlayer>(); 
    public static Dictionary<string, SideResource> fieldRes = new Dictionary<string, SideResource>();
    public static Dictionary<string, SideMonster> fieldMon = new Dictionary<string, SideMonster>();
    public static Dictionary<string, SideBoss> fieldBoss = new Dictionary<string, SideBoss>();
    public static List<LineInfo> lineInfos = new List<LineInfo>();
    public static SCGetLineInfo lineInfo;
    public static int currLine;
    public static bool beSend=false;//被动传回来
    public static string challenger = "";//挑战者
    public static SCSidePveFightEnd pveEnd;
    public static SidePvpMV sidePvpMv;
    public static SCSideGetPvpMV sideGetPvpMv;
    public static bool netBreak = false;
    public static string pveBossUUID = "";
    public static SCSidePveBossFightEnd pveBossEnd;
    public static SideMonster currClickMonster = null;
    public static SideBoss currClickBoss = null;
    public static int tirednessCost = -1;
    public static void ClearFieldCache()
    {
        playerInfos.Clear();
        fieldRes.Clear();
        fieldMon.Clear();
        fieldBoss.Clear();
        lineInfos.Clear();
    }
    #endregion

    #region 王者招募
    public static bool isOpeningKingsRecruit = false;
    public static WarriorRecruit kingRecruit = null;
    #endregion


    #region 黑名单
    public static List<SimplePlayerInfo> blackList = new List<SimplePlayerInfo>();
    public static void RefreshBlackList(List<SimplePlayerInfo> list)
    {
        blackList.Clear();
        blackList.AddRange(list);
    }
    public static bool IsPlayerInBlackList(long playerId, int serverId)
    {
        foreach (var info in blackList)
        {
            if (info.playerId == playerId && info.serverId == serverId)
            {
                return true;
            }
        }
        return false;
    }
    #endregion

    #region Union
    public static bool heroOpen;
    public static bool treeOpen;
    #endregion


    #region 导航精灵
    public static FuncNavKit funcNavKit = new FuncNavKit();
    #endregion

    #region 公会战
    public static UnionFightData unionFightData = new UnionFightData();
    public static UIUnionData uiUnionFightData = new UIUnionData();
    #endregion

}